<?php
/**
 * Created by PhpStorm.
 * @author: LIZHEN
 * @version $Id: View.php 19582 13-12-13 上午11:16 jieyin $
 * @package 
 */

tp_include(FRAMEWORK_PATH . '/Functions/template.func.php');
tp_include(FRAMEWORK_PATH . '/View/Template.class.php');

class View extends Base {

    /**
     * 模板输出变量
     * @var tVar
     * @access protected
     */
    protected $tVar     =   array();

    const VIEW_BASE_PATH = '/Views/';

    /**
     * 模板变量赋值
     * @access public
     * @param mixed $name
     * @param mixed $value
     */
    public function assign($name,$value=''){
        if(is_array($name)) {
            $this->tVar   =  array_merge($this->tVar,$name);
        }else {
            $this->tVar[$name] = $value;
        }
    }

    public function display($file) {
        ob_start();
        ob_implicit_flush(false);
        extract($this->tVar, EXTR_OVERWRITE);
        include $this->template($file);
        $contents = ob_get_clean();
        $this->_output($contents);
        exit();
    }

    /**
     * 模板输出(辅助)
     * @access protected
     * @param string $content 要输出的内容
     * @return void
     */
    protected function _output($content) {
        echo $content;
    }

    protected function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $cachefile, $tpldir, $file) {
        static $tplrefresh, $timestamp;
        if($tplrefresh === null) {
            $tplrefresh = C('view=>tplrefresh');
            $timestamp = C('view=>timestamp');
        }
        //Logger::debug("maintpl : %s <br /> subtpl : %s <br /> timecompare : %s <br /> templateid : %s <br /> cachefile : %s <br /> tpldir : %s<br /> file : %s <br />",$maintpl, $subtpl, $timecompare, $templateid, $cachefile, $tpldir, $file);
        if(empty($timecompare) || $tplrefresh == 1 || ($tplrefresh > 1 && !($timestamp % $tplrefresh))) {
            if( C('debug') || empty($timecompare) || @filemtime($subtpl) > $timecompare) {
                $template = new template();
                $template->parse_template($maintpl, $templateid, $tpldir, $file, $cachefile);
                return TRUE;
            }
        }
        return FALSE;
    }

    protected function template($file, $templateid = 0, $tpldir = '', $gettplfile = 0, $primaltpl='') {

        $tpldir = $tpldir ? $tpldir : (defined('TPLDIR') ? TPLDIR : '');
        $templateid = $templateid ? $templateid : (defined('TEMPLATEID') ? TEMPLATEID : '');
        $filebak = $file;

        if(!$tpldir) {
            $tpldir = str_replace(APP_PATH,'',MODULES_PATH.'/Views');
        }
        $tplfile = $tpldir.'/'.$file.'.html';

        //Logger::debug("APP_PATH : %s <br>MODULES_PATH : %s <br /> CACHE_PATH : %s <br> tplfile : %s", APP_PATH, MODULES_PATH, CACHE_PATH, $tplfile);
        $cachefile = CACHE_PATH.'/'.(defined('STYLEID') ? STYLEID.'_' : '_').$templateid.'_'.str_replace('/', '_', $file).'.tpl.php';
        if($templateid != 1 && !file_exists($tplfile) && !file_exists(substr($tplfile, 0, -4).'.php')
            && !file_exists(($tplfile = $tpldir.$filebak.'.htm'))) {
            $tplfile = MODULES_PATH.'/Views/'.$filebak.'.html';
        }

        if($gettplfile) {
            return $tplfile;
        }
        $this->checktplrefresh($tplfile, $tplfile, @filemtime($cachefile), $templateid, $cachefile, $tpldir, $file);
        return $cachefile;
    }
}